package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.*;
import com.dongdongshop.admin.vo.GoodsDescVO;
import com.dongdongshop.admin.vo.GoodsVO;
import com.dongdongshop.admin.vo.ItemVO;
import com.dongdongshop.api.ItemCatServiceApi;
import com.dongdongshop.common.Result;
import com.dongdongshop.dto.ItemCatDTO;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private ItemCatServiceApi itemCatServiceApi;
    //事务发生异常就回滚
    @Transactional
    @Override
    public void insertInfo(GoodsVO goodsVO, GoodsDescVO goodsDescVO, List<ItemVO> itemVOList) {
        try {
            TbGoods tbGoods = new TbGoods();
            BeanUtils.copyProperties(goodsVO, tbGoods);
            //增加返回主键
            tbGoodsMapper.insertInfo(tbGoods);
            //获取返回的主键id增加商品详细表
            TbGoodsDesc tbGoodsDesc = new TbGoodsDesc();
            BeanUtils.copyProperties(goodsDescVO,tbGoodsDesc);
            tbGoodsDesc.setGoodsId(tbGoods.getId());
            tbGoodsDescMapper.insert(tbGoodsDesc);
            TbItem items = new TbItem();
            List<TbItem> collect = itemVOList.stream().map(i -> {
                TbItem item = new TbItem();
                BeanUtils.copyProperties(i, item);
                return item;
            }).collect(Collectors.toList());
            //加item表
            for (TbItem item : collect) {
                item.setGoodsId(tbGoods.getId());
                item.setCategoryid(tbGoods.getCategory3Id());
                item.setStatus("0");
                item.setCreateTime(new Date());
                item.setUpdateTime(new Date());
                item.setSellerId(tbGoods.getSellerId());
            }
            itemMapper.insertAll(collect);

            //根据goodsid查询itemd
            TbItemExample example = new TbItemExample();
            TbItemExample.Criteria criteria = example.createCriteria();
            criteria.andGoodsIdEqualTo(tbGoods.getId());
            List<TbItem> tbItems = itemMapper.selectByExample(example);

            TbItem tbItem = tbItems.get(0);
            tbGoods.setDefaultItemId(tbItem.getId());

            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        }catch (Exception e) {
            throw e;
        }

    }

    @Override
    public List<GoodsVO> getAll(String goodsName) {
        TbGoodsExample e = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = e.createCriteria();
        criteria.andAuditStatusNotEqualTo("1");
        if(StringUtils.isNotBlank(goodsName)){
            criteria.andGoodsNameLike("%"+goodsName + "%");
        }
        Result all = itemCatServiceApi.getAll();
        List<ItemCatDTO> itemCatVOList = (List<ItemCatDTO>) all.getData();
        List<TbGoods> tbGoods = tbGoodsMapper.selectByExample(e);
        return tbGoods.stream().map(v -> {
            GoodsVO vo = new GoodsVO();
            BeanUtils.copyProperties(v, vo);
            vo.setItemCatVOList(itemCatVOList);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public GoodsVO auditYes(Long[] ids) {
        GoodsVO goodsVO = new GoodsVO();
        for (Long id : ids) {
            TbGoods t = tbGoodsMapper.selectByPrimaryKey(id);
            t.setAuditStatus("1");
            tbGoodsMapper.updateByPrimaryKeySelective(t);
            BeanUtils.copyProperties(t, goodsVO);
        }
        return goodsVO;
    }

    @Override
    public GoodsVO auditNo(Long[] ids) {
        GoodsVO goodsVO = new GoodsVO();
        for (Long id : ids) {
            TbGoods t = tbGoodsMapper.selectByPrimaryKey(id);
            t.setAuditStatus("2");
            tbGoodsMapper.updateByPrimaryKeySelective(t);
            BeanUtils.copyProperties(t, goodsVO);
        }
        return goodsVO;
    }

    @Override
    public GoodsVO getById(Long id) {
        TbGoodsDesc tbGoodsDesc = tbGoodsDescMapper.selectByPrimaryKey(id);
        GoodsDescVO vo = new GoodsDescVO();
        BeanUtils.copyProperties(tbGoodsDesc, vo);
        //查询三级分类
        GoodsVO goodsVO = new GoodsVO();
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(tbGoods, goodsVO);

        goodsVO.setGoodsDescVO(vo);

        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        example.setOrderByClause("is_default desc");
        List<TbItem> tbItems = itemMapper.selectByExample(example);
        List<ItemVO> itemVOList = tbItems.stream().map(item -> {
            ItemVO itemVO = new ItemVO();
            BeanUtils.copyProperties(item, itemVO);
            return itemVO;
        }).collect(Collectors.toList());

        goodsVO.setItemVOList(itemVOList);
        Result<ItemCatDTO> itemCat1 = itemCatServiceApi.getId(goodsVO.getCategory1Id());
        Result<ItemCatDTO> itemCat2 = itemCatServiceApi.getId(goodsVO.getCategory2Id());
        Result<ItemCatDTO> itemCat3 = itemCatServiceApi.getId(goodsVO.getCategory3Id());
        if (itemCat1.getCode() == 10000 && itemCat1.getData()!=null) {
            goodsVO.setCategory1Name(itemCat1.getData().getName());
        }
        if (itemCat2.getCode() == 10000 && itemCat2.getData()!=null) {
            goodsVO.setCategory2Name(itemCat2.getData().getName());
        }
        if (itemCat3.getCode() == 10000 && itemCat3.getData()!=null) {
            goodsVO.setCategory3Name(itemCat3.getData().getName());
        }
        return goodsVO;
    }
}
